package mapEditor.interactor;

import debug.*;
	
import mapEditor.map.MapCanvas;
import mapEditor.obj.DrawObject;
import mapEditor.utils.MapLocation;

import java.awt.Graphics2D;


// ********************************************************************
// *** CLASS: EditMouseAdapter
// ********************************************************************
public class EditInteractor extends MapInteractorAdapter
{ public String getMouseModeMnemonic() { return("Edit"); }
  private DrawObject.Mark draggingMark; // !null when dragging a Mark
  private DrawObject draggingObject;    // !null when dragging a DrawObject

  // ******************************************************************
  // *** CONSTRUCTOR: EditMouseAdapter
  // ******************************************************************
  public EditInteractor(MapCanvas mapCanvas)
  { super(mapCanvas,EDIT);  
  }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g)
  {	if(draggingObject!=null)
	{ double dx=mouseCurrent.getX()-mouseStart.getX();
	  double dy=mouseCurrent.getY()-mouseStart.getY();
	  draggingObject.paintContour((Graphics2D)g,dx,dy);
	}
  }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  { if(draggingObject!=null) mapCanvas.repaint();
    if(draggingMark!=null) { draggingMark.moveTo(mouseCurrent.getX(),mouseCurrent.getY()); mapCanvas.repaint(); }
  }


  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(MapLocation p)
  { if(draggingMark==null && draggingObject==null)
    { mouseStart=p;
      draggingMark=mapCanvas.getMark(mouseCurrent);
	  if(draggingMark==null) draggingObject=mapCanvas.getObject(mouseCurrent);
    }
  }
  protected void mouseRightPressed(MapLocation p)
  {
  }

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(MapLocation p)
  { if(draggingObject!=null)
	{ double dx=mouseCurrent.getX()-mouseStart.getX();
	  double dy=mouseCurrent.getY()-mouseStart.getY();
      draggingObject.move(dx,dy);
      draggingObject=null;
	  mapCanvas.repaint();
    }
    draggingMark=null;
  }
  protected void mouseRightReleased(MapLocation p)
  { DrawObject.Mark mark=mapCanvas.getMark(p);
    if(mark!=null) 
    { Debug.TRACE("EditMouseAdapter.mouseRightReleased: WithinMark="+mark);
      mark.edit();	
    }
    else
    { DrawObject obj=mapCanvas.getObject(p);
      if(obj!=null) 
      { Debug.TRACE("EditMouseAdapter.mouseRightReleased: WithinObject="+obj);
        mapCanvas.setSelected(obj);
        obj.edit();
      }
    }
  }

}